package com.imooc.framework.web;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

@Slf4j
@RequestMapping("thread")
@RestController
public class ThreadController {

    private static final ReentrantReadWriteLock reentrantLock = new ReentrantReadWriteLock();
    private static final Lock rlock = reentrantLock.readLock();
    private static final Lock wlock = reentrantLock.writeLock();
//
//    @GetMapping("reentrantLock")
//    public String reentrantLock(){
//
//        try {
//            log.info(Thread.currentThread().getName() + " start.....");
//            wlock.lock();
//            log.info(Thread.currentThread().getName() + " running");
//            Thread.sleep(1000);
//            log.info(Thread.currentThread().getName() + " ending");
//        }catch (Exception e){
//            log.info(e.getMessage());
//        }finally {
//            wlock.unlock();
//        }
//        return "success";
//    }

//    @GetMapping("reentrantLock1")
//    public String reentrantLock1(){
//
//        try {
//            log.info(Thread.currentThread().getName() + " start.....");
////            rl.lock();
//            log.info(Thread.currentThread().getName() + " running");
//            Thread.sleep(100);
//            log.info(Thread.currentThread().getName() + " ending ");
//        }catch (Exception e){
//            log.info(e.getMessage());
//        }finally {
////            rl.unlock();
//        }
//        return "success";
//    }

//    @GetMapping("reentrantLock2")
//    public String reentrantLock2(){
//
//        try {
////            rl.lock();
//            log.info(Thread.currentThread().getName() + " no get lock");
////            Thread.sleep(100);
//            log.info(Thread.currentThread().getName() + " no get lock second");
//        }catch (Exception e){
//            log.info(e.getMessage());
//        }finally {
////            rl.unlock();
//        }
//        return "success";
//    }
}
